package com.linewell.dao;

import com.linewell.entity.Article;
import com.linewell.entity.Tag;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Title: ssm2
 * @description:
 * @author: hyuhuang@linewell.com
 * @since:2018/2/5
 */
public interface TagMapper {
   // @Select("select tag_id,tag_name from tag where tag_id not in (1001,1002,1003)")
    @ResultMap("com.linewell.dao.TagMapper.Tag")
    @Select("select t.tag_id,tag_name,count(*) num " +
            "from tag t,multilabel m,article a " +
            "where t.tag_id = m.tag_id " +
            "and m.type = 2 " +
            "and m.lable_id = a.article_id " +
            "and a.acheck = 1 " +
            "and t.tag_id not in (1001,1002,1003) group by t.tag_id,tag_name order by num Desc")
    List<Tag> getHotTag();

    @ResultMap("com.linewell.dao.TagMapper.Tag")
    @Select("select tag_id,count(*) num from multilabel where tag_id not in (1001,1002,1003) group by tag_id order by num DESC ")
    List<Tag> getCountTag();

    @Select("select * from tag where tag_id not in (1001,1002,1003)")
    List<Tag> getTags();


    @Insert("insert into tag values ('${tag}')")
    void insertTag(@Param("tag") String tag);

    @Select("select * from tag where tag_name = '${tag}'")
    List<Tag> selectTagByTagName(@Param("tag")String tag);

    @Insert("insert into multilabel values (${articleId},${tagId},2)")
    void insertMutil(@Param("articleId")Long articleId, @Param("tagId")int tagId);

    @Select("select count(*) from multilabel where lable_id = ${userId} AND type =1")
    int selectUserTag(@Param("userId")Long userId);

    @Insert("insert into multilabel values (${articleId},${tagId},2)")
    void insertMutilByArticle(@Param("articleId")long articleId, @Param("tagId")int tagId);
    @Insert("insert into multilabel values (${userId},${tagId},1)")
    void insertUserTag(@Param("userId")Long userId, @Param("tagId")int tagId);
    @Select("select id FROM multilabel where lable_id = ${userId} ORDER BY newid()")
    Long[] selectTagId(@Param("userId")Long userId);
    @Update("Update multilabel set tag_id = ${tagId} where id = ${tId}")
    void updateUserTag(@Param("tId")Long tId, @Param("tagId")int tagId);

    @Select("select t.* from tag t,multilabel m where m.lable_id = 1 and m.tag_id = t.tag_id;")
    List<Tag> getTagByArticle(Article article);
}
